package view;

import control.KasseController;
import java.awt.AWTException;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import utility.ImageToolbox;
import utility.ScreenImage;
import view.kasse.BetalingsPanel;

/**
 *
 * @author Maja Gliese
 */
public class MenuPanel extends javax.swing.JPanel {

    private final MainGUI mainGUI;

    /**
     * Creates new form MenuPanel
     *
     * @param mainGUI
     */
    public MenuPanel(MainGUI mainGUI) {
        initComponents();
        setSize(609, 380);
        this.mainGUI = mainGUI;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        statistikKnap = new javax.swing.JButton(){
            public void paint(Graphics g){
                ImageToolbox.drawImage(g, (ImageIcon)statistikKnap.getIcon(), 0, 0, statistikKnap.getWidth(), statistikKnap.getHeight());
            }
        };
        lagerKnap = new javax.swing.JButton(){
            public void paint(Graphics g){
                ImageToolbox.drawImage(g, (ImageIcon)lagerKnap.getIcon(), 0, 0, lagerKnap.getWidth(), lagerKnap.getHeight());
            }
        };
        salgsHisKnap = new javax.swing.JButton(){
            public void paint(Graphics g){
                ImageToolbox.drawImage(g, (ImageIcon)salgsHisKnap.getIcon(), 0, 0, salgsHisKnap.getWidth(), salgsHisKnap.getHeight());
            }
        };
        kasseKnap = new javax.swing.JButton(){
            public void paint(Graphics g){
                ImageToolbox.drawImage(g, (ImageIcon)kasseKnap.getIcon(), 0, 0, kasseKnap.getWidth(), kasseKnap.getHeight());
            }
        };
        jLabel2 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setFocusCycleRoot(true);
        setOpaque(false);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });
        setLayout(null);

        statistikKnap.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/StatistikModul1.PNG"))); // NOI18N
        statistikKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                statistikKnapActionPerformed(evt);
            }
        });
        add(statistikKnap);
        statistikKnap.setBounds(190, 290, 180, 180);

        lagerKnap.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/stock_control.png"))); // NOI18N
        lagerKnap.setText("Lager");
        lagerKnap.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        lagerKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lagerKnapActionPerformed(evt);
            }
        });
        add(lagerKnap);
        lagerKnap.setBounds(190, 100, 180, 180);

        salgsHisKnap.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/KasserapportModul.png"))); // NOI18N
        salgsHisKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salgsHisKnapActionPerformed(evt);
            }
        });
        add(salgsHisKnap);
        salgsHisKnap.setBounds(380, 290, 190, 180);

        kasseKnap.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/imackasse.png"))); // NOI18N
        kasseKnap.setText("Kasse");
        kasseKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                kasseKnapActionPerformed(evt);
            }
        });
        add(kasseKnap);
        kasseKnap.setBounds(380, 100, 190, 180);

        jLabel2.setBackground(new java.awt.Color(255, 255, 255));
        jLabel2.setOpaque(true);
        add(jLabel2);
        jLabel2.setBounds(390, 110, 160, 130);
    }// </editor-fold>//GEN-END:initComponents

    private void lagerKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lagerKnapActionPerformed
        mainGUI.showPage("LagerPanel");
    }//GEN-LAST:event_lagerKnapActionPerformed

    private void salgsHisKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salgsHisKnapActionPerformed
        mainGUI.showPage("KasseRapportPanel");
    }//GEN-LAST:event_salgsHisKnapActionPerformed

    private void kasseKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_kasseKnapActionPerformed
        try {
            if (!KasseController.getInstance().setDagsKasse()) {
                Rectangle r = new Rectangle(getLocationOnScreen(), getSize());
                ImageIcon img = null;
                try {
                    img = new ImageIcon(ScreenImage.createImage(r));
                } catch (AWTException ex) {
                    Logger.getLogger(BetalingsPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
                mainGUI.visKasseBeholdningsPanel(true, img);
            } else {
                mainGUI.showPage("KassePanel");
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Fejl 1001 \nFejl ved indlæsning af kasse fra databasen. Prøv venligst igen senere. \n" + ex.getLocalizedMessage(), "Database Timeout", 0);
        } catch (ParseException ex) {
            JOptionPane.showMessageDialog(this, "Fejl 1025 \nFejl ved formatering af dato. \n" + ex.getLocalizedMessage(), "Fejl", 0);
        }
    }//GEN-LAST:event_kasseKnapActionPerformed

    private void statistikKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_statistikKnapActionPerformed
        mainGUI.showPage("StatistikPanel");
    }//GEN-LAST:event_statistikKnapActionPerformed

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        this.requestFocus();
    }//GEN-LAST:event_formComponentShown

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_1) {
            mainGUI.showPage("LagerPanel");
        } else if (evt.getKeyCode() == KeyEvent.VK_2) {
            try {
                if (!KasseController.getInstance().setDagsKasse()) {
                    Rectangle r = new Rectangle(getLocationOnScreen(), getSize());
                    ImageIcon img = null;
                    try {
                        img = new ImageIcon(ScreenImage.createImage(r));
                    } catch (AWTException ex) {
                        Logger.getLogger(BetalingsPanel.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    mainGUI.visKasseBeholdningsPanel(true, img);
                } else {
                    mainGUI.showPage("KassePanel");
                }
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(this, "Fejl 1001 \nFejl ved indlæsning af kasse fra databasen. Prøv venligst igen senere. \n" + ex.getLocalizedMessage(), "Database Timeout", 0);
            } catch (ParseException ex) {
                JOptionPane.showMessageDialog(this, "Fejl 1025 \nFejl ved formatering af dato. \n" + ex.getLocalizedMessage(), "Fejl", 0);
            }
        } else if (evt.getKeyCode() == KeyEvent.VK_3) {
            mainGUI.showPage("StatistikPanel");
        } else if (evt.getKeyCode() == KeyEvent.VK_4) {
            mainGUI.showPage("KasseRapportPanel");
        }
    }//GEN-LAST:event_formKeyPressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel2;
    private javax.swing.JButton kasseKnap;
    private javax.swing.JButton lagerKnap;
    private javax.swing.JButton salgsHisKnap;
    private javax.swing.JButton statistikKnap;
    // End of variables declaration//GEN-END:variables
}
